package com.company;

public class Main {
    public static int numDecodings(String s){
        int l = s.length();
        if(l == 0 || s.charAt(0) == '0')return 0;
        if(l == 1)return 1;
        int [] result = new int[l+2];
        result[0] = 0;
        result[1] = result[2] = 1;
        for(int i=1;i<l;i++){
            if(Integer.parseInt(s.substring(i-1,i+1)) <= 26){
                if(s.charAt(i) == '0'){
                    if(i>=2 && s.charAt(i-2) == '0'){
                        result[i+2] = result[i+1];
                    }else{
                        if(i>=2&&s.charAt(i-2)>2){
                            result[i+2] = result[i+1];
                        }else{
                            result[i+2] = result[i+1] - result[i-1];
                        }

                    }
                }else{
                    if(s.charAt(i-1) == '0'){
                        result[i+2] = result[i+1];
                    }else{
                        result[i+2] = result[i+1] + result[i];
                    }
                }
            }else{
                if(s.charAt(i) == '0'){
                    return 0;
                }else{
                    result[i+2] = result[i+1];
                }
            }
        }
        return result[l+1];
    }

    public static int numDecoding1(String s){
        if(s.charAt(0) == '0')return 0;
        int pre = 1,curr = 1;
        int l = s.length();

        for(int i=1;i<l;i++){
            int temp = curr;
            if(s.charAt(i) == '0'){
                if(s.charAt(i-1) == '1' || s.charAt(i-1) == '2')curr = pre;
                else return 0;
            }
            else if(s.charAt(i-1) == '1' || (s.charAt(i-1)=='2'&&s.charAt(i)<='6')){
                curr = curr + pre;
            }
            pre = temp;
        }
        return curr;
    }

    public static void main(String[] args) {
	// write your code here
        System.out.println(numDecoding1("12"));

    }
}
